package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.InstallBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class InstallSearchActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_search;
    private RecyclerView search_recycler;

    private List<InstallBean.InfoEntity> info1 = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_search);
        et_search = (EditText) findViewById(R.id.et_search);
        search_recycler = (RecyclerView) findViewById(R.id.recycler);
        search_recycler.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));

        findViewById(R.id.cancel).setOnClickListener(this);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    upSearch();
                    return true;
                }
                return false;
            }
        });
    }

    private void upSearch() {
        if ("".equals(et_search.getText().toString().trim())) {
            MyToast("请输入内容");
            return;
        }
        Map<String, Object> map = new Hashtable<>();
        map.put("keywords", et_search.getText().toString());
        XUtil.Post(Url.DECORATE_INDEX, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        InstallBean bean = gson.fromJson(result, InstallBean.class);
                        info1 = bean.getInfo();
                        setData();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
        }
    }

    private void setData() {
        search_recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_install;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                if (position % 2 == 0) {
                    holder.getView(R.id.right).setVisibility(View.GONE);
                } else {
                    holder.getView(R.id.right).setVisibility(View.VISIBLE);
                }
                holder.setTextView(R.id.name, info1.get(position).getName());
                x.image().bind(holder.getImageView(R.id.img), info1.get(position).getLogo_pic());

                holder.setTextView(R.id.price, "￥" + info1.get(position).getPrice());

                holder.getView(R.id.rl_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if ("0".equals(info1.get(position).getType())) {
                            startActivity(new Intent(InstallSearchActivity.this, InstallDetails0Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()));
                        } else {
                            startActivity(new Intent(InstallSearchActivity.this, InstallDetails1Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()));
                        }
                        finish();
                    }
                });

            }

        });
    }
}
